|
In object-oriented programming, a friend function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public .〔 〕 Normally, a function that is defined outside of a class cannot access such information. Declaring a function a friend of a class allows this, in languages where the concept is supported. A friend function is declared by the class that is granting access, explicitly stating what function from a class is allowed access. A similar concept is that of friend class. Friends should be used with caution. Too many functions or external classes declared as friends of a class with protected or private data may lessen the value of encapsulation of separate classes in object-oriented programming and may indicate a problem in the overall architecture design. Generally though, friend functions are a good thing for encapsulation, as you can keep data of a class private from all except those who you explicitly state need it, but this does mean your classes will become tightly coupled. ==Use cases== This approach may be used in friendly function when a function needs to access private data in objects from two different classes. This may be accomplished in two similar ways *a function of global or namespace scope may be declared as friend of both classes *a member function of one class may be declared as friend of another one. 抄文引用元・出典: フリー百科事典『 ウィキペディア(Wikipedia)』 ■ウィキペディアで「friend function」の詳細全文を読む スポンサード リンク
|